package _021_10_6

import "sort"

func longestCommonPrefix2(strs []string) string {
	if len(strs) == 0 {
		return ""
	}
	if len(strs) == 1 {
		return strs[0]
	}
	k := 0
	for {
		for i := 1; i < len(strs); i++ {
			if len(strs[0]) == k || k == len(strs[i]) || strs[i][k] != strs[0][k] {
				return string([]byte(strs[0])[0:k])
			}
		}
		k++
	}
}
func longestCommonPrefix(strs []string) string {
	// write code here
	//sort.Strings(str[])
	//sort.Sort(byte(strs[0]))
	result := make([][]string, len(strs))

	for i := 0; i < len(strs); i++ {
		for j := 0; j < len(strs[i]); j++ {
			result[i] = append(result[i], string(strs[i][j]))
			sort.Strings(result[i])
		}
	}
	c := ""
	k := 0
	for {
		c = result[0][k]
		for i := 0; i < len(result); i++ {
			if result[i][k] != c {
				ret := ""
				for z := 0; z < k; z++ {
					ret += result[0][z]
				}
				return ret
			}
		}
		k++
	}
}
